﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.BLL;

namespace tracker.Controllers
{
    public class HandlerADUStateDefineController : Controller
    {
        /// <summary>
        /// GET HandlerADUStateDefine 
        /// </summary>
        /// <param name="strAlarmType">Alarm Type</param>
        /// <param name="strExpression">Expression</param>
        /// <param name="strHandlerType">Add/Update</param>
        /// <returns>true/false</returns>
        [CallAjaxFilter]
        public string Index(string DefineID, string BitIndex, string Expression, string true_exp, string false_exp, string HandlerType)
        {
            string strDefineID = DefineID;
            string strBitIndex = BitIndex;
            string strExpression = Expression;
            string strTrueExp = true_exp;
            string strFalseExp = false_exp;
            string strHandlerType = HandlerType;

            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            if (HandlerType == "Add")
            {
                EStatusDefine ent = new EStatusDefine();
                ent.CreateBy = _username;
                ent.BitIndex = strBitIndex;
                ent.false_exp = strFalseExp;
                ent.true_exp = strTrueExp;
                ent.Expression = strExpression;

                string strResult = StatusDefineBLL.InsertERP(new string[]{ EStatusDefine.FIED_CREATEBY,
                                                                          EStatusDefine.FIED_BITINDEX,
                                                                          EStatusDefine.FIED_EXPRESSION,
                                                                          EStatusDefine.FIED_FALSE_EXP,
                                                                          EStatusDefine.FIED_TRUE_EXP
                                                            },
                                                            new object[]{ ent.CreateBy,
                                                                          ent.BitIndex,
                                                                          ent.Expression,
                                                                          ent.false_exp,
                                                                          ent.true_exp
                                                            });
                if (strResult == "0")
                    return strResult;
                else
                    return "false";
            }
            else if (strHandlerType == "Update")
            {
                EStatusDefine ent = new EStatusDefine();
                ent.ModifyBy = _username;
                ent.CreateBy = _username;
                ent.BitIndex = strBitIndex;
                ent.false_exp = strFalseExp;
                ent.true_exp = strTrueExp;
                ent.Expression = strExpression;
                ent.DefineID = strDefineID;
                ent.CreateBy = _username;

                string strResult = StatusDefineBLL.UpdateERP(new string[]{ EStatusDefine.FIED_MODIFYBY,
                                                                           EStatusDefine.FIED_BITINDEX,
                                                                           EStatusDefine.FIED_EXPRESSION,
                                                                           EStatusDefine.FIED_ID,
                                                                           EStatusDefine.FIED_CREATEBY,
                                                                           EStatusDefine.FIED_FALSE_EXP,
                                                                           EStatusDefine.FIED_TRUE_EXP
                                                            },
                                                            new object[]{  ent.ModifyBy,
                                                                           ent.BitIndex,
                                                                           ent.Expression,
                                                                           ent.DefineID,
                                                                           ent.CreateBy,
                                                                           ent.false_exp,
                                                                           ent.true_exp
                                                            });

                if (strResult == "0")
                    return "true";
                else
                    return "false";
            }
            else if (strHandlerType == "Delete")
            {
                string strResult = StatusDefineBLL.DeleteERP(new string[] { EStatusDefine.FIED_CREATEBY, EStatusDefine.FIED_ID },
                                                            new object[] { _username, strDefineID });

                if (strResult == "0")
                    return "true";
                else
                    return "false";
            }
            else
            {
                return "false";
            }
        }
    }
}
